package com.shop2cn.iapi.agentproducts.testcase;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AgentPrice;
import com.shop2cn.iapi.agentproducts.parameter.AgentPriceDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.parameter.UpdatePriceDto;
import com.shop2cn.iapi.agentproducts.parameter.UpdateStockAndPriceBean;
import com.shop2cn.iapi.agentproducts.service.UpdateStockAndPriceCall;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.ExcleUtil;

/**
 * 通过sku更新库存价格代理价(自采商品)Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateStockAndPrice {
	private static UpdateStockAndPriceBean updatestockandpriceBean;
	private static UpdateStockAndPriceCall updatestockandpriceCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通过sku更新库存价格代理价(自采商品)Test");
	}

	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();

	@Before
	public void caseUp() {
		updatestockandpriceBean = new UpdateStockAndPriceBean();
		updatestockandpriceCall = new UpdateStockAndPriceCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatestockandpriceCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateStockAndPrice_001() {
		Logger.start(true, "渠道商根据sku修改价格，发送总线--验证代理价格");
		try {
			int rootUserId = 501475646;
			int agentUserId = 501475647;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "",
					true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			// 查到代理商的商品id
			List<AgentProductPo> ap = ymtProductsIWapper.selectAgentProductByRootProductId(productId).stream()
					.filter(x -> x.getAction() == 0).filter(x -> x.getUserId() == agentUserId)
					.collect(Collectors.toList());
			Logger.debug("ap:" + FormatUtil.beanToGSONString(ap));
			String agentProductId = ap.get(0).getProductId();
			updatestockandpriceBean.setUserId(rootUserId);
			updatestockandpriceBean.setSkuId(addAgentProductBean.getCatalogList().get(0).getSku());
			updatestockandpriceBean.setSalePrice(500d);
			AgentPrice agentPrice = new AgentPrice(); 
			agentPrice.setAgentPrice(350);
			
			agentPrice.setAgentTypeName("一级代理");
			updatestockandpriceBean.setAgentPriceList(Arrays.asList(agentPrice));
			updatestockandpriceCall.setData(updatestockandpriceBean);
			updatestockandpriceCall.callService();
			Logger.verifyEquals("200", updatestockandpriceCall.getString("code"), "验证code");

			// 验证总线消息
			MessageListBean MessageListBean = MqQueryCallService.initSyncAutoMarkup(agentProductId, agentUserId);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			Logger.verifyEquals(joa.size(), 1, "有更新价格的总线消息");
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});

			Thread.sleep(5000);

			ProductDetailsDto agentProd = AddAgentProductCallService.selectProductDetails(agentUserId, agentProductId);

			double agentprice = agentProd.getCatalogList().get(0).getAgentPrice();
			Logger.verifyEquals(agentprice, 350.00, "验证代理价格已经被修改");
			//代理价加价百分之三十三 =直销价 （保留一位小数）
			String format = new DecimalFormat("#.0").format(agentProd.getCatalogList().get(0).getAgentPrice()*1.33);
			Logger.verifyEquals(format,
					""+agentProd.getCatalogList().get(0).getDirectPrice(), "");
			// 商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(), 1, "已经被打上自动加价的标");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateStockAndPrice_002() {
		Logger.start(true, "渠道商根据sku修改库存");
		try {
			int rootUserId = 501475646;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "",
					true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			// 查到代理商的商品id
			updatestockandpriceBean.setUserId(rootUserId);
			updatestockandpriceBean.setSkuId(addAgentProductBean.getCatalogList().get(0).getSku());
			updatestockandpriceBean.setSalePrice(500d);
			updatestockandpriceBean.setRealStock(400);
			updatestockandpriceBean.setVirtualStock(300);
			updatestockandpriceCall.setData(updatestockandpriceBean);
			updatestockandpriceCall.callService();
			Logger.verifyEquals("200", updatestockandpriceCall.getString("code"), "验证code");
			Thread.sleep(3000);
			ProductDetailsDto agentProd = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getStock(), 700, "验证总库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getRealStock(), 400, "验证真实库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getVirtualStock(), 300, "验证虚拟库存");
			updatestockandpriceBean.setRealStock(null);
			updatestockandpriceBean.setVirtualStock(400);
			updatestockandpriceCall.setData(updatestockandpriceBean);
			updatestockandpriceCall.callService();
			Logger.verifyEquals("200", updatestockandpriceCall.getString("code"), "验证code");
			Thread.sleep(3000);
			agentProd = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getStock(), 800, "验证总库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getRealStock(), 400, "验证真实库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getVirtualStock(), 400, "验证虚拟库存");
			updatestockandpriceBean.setRealStock(0);
			updatestockandpriceBean.setVirtualStock(350);
			updatestockandpriceCall.setData(updatestockandpriceBean);
			updatestockandpriceCall.callService();
			Logger.verifyEquals("200", updatestockandpriceCall.getString("code"), "验证code");
			Thread.sleep(3000);
			agentProd = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getStock(), 350, "验证总库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getRealStock(), 0, "验证真实库存");
			Logger.verifyEquals(agentProd.getCatalogList().get(0).getVirtualStock(), 350, "验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateStockAndPrice_003() {
		Logger.start(true, "渠道商根据sku修改限购数");
		try {
			int rootUserId = 501475646;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "",
					true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			// 查到代理商的商品id
			updatestockandpriceBean.setUserId(rootUserId);
			updatestockandpriceBean.setSkuId(addAgentProductBean.getCatalogList().get(0).getSku());
			updatestockandpriceBean.setLimitNum(3);
			updatestockandpriceBean.setSaleStatus(2);
			updatestockandpriceCall.setData(updatestockandpriceBean);
			updatestockandpriceCall.callService();
			Logger.verifyEquals("200", updatestockandpriceCall.getString("code"), "验证code");
			Thread.sleep(3000);
			
			Logger.verifyEquals(3, ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getiLimitNum(), "验证库存限购");
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(rootUserId);
			Logger.verifyEquals(2,getProductLists.get(0).getStatus(),"验证上下架");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
}